<?php
namespace Home\Model;
use Think\Model;
class MemberModel extends Model {
	public $table='zjy_member';
	public $page; //当前页
	public $perpage; //每页显示
	public $member_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词

	//提交
	public function user_add_post()
	{
		$Model=M("Member");
		$data=$this->post;
		if($Model->add($data)){
			//同步写入company表
			//$member_id=$Model->where("member_email='".$data['member_email']."'")->getField('member_id');
			$Model1=M("Company");
			$data['member_id']=$data['member_id'];
			$Model1->add($data);
			return true;
		}else{
			return false;
		}
	}
	//编辑
	public function user_edit_post()
	{
		$Model=M("Member");
		$condition['member_id'] = $this->post['member_id'];
		$data=$this->post;
		// dump($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	
	//判断邮箱用户是否登录成功 查询本地用户
	public function check_email_login()
	{
		$Model=M("Member");
		$condition['member_email']=$this->post['member_email'];
		$condition['member_passwd']=$this->post['member_passwd'];
		$condition['member_state']=1; //1为启用2为关闭
		$condition['_logic'] = 'and';
		$count=$Model->where($condition)->count();
		if($count==0){
			return false;
		}
		return true;
	}

	//判断手机用户是否登录成功 查询本地用户
	public function check_mobile_login()
	{
		$Model=M("Member");
		$condition['member_mobile']=$this->post['member_mobile'];
		$condition['member_passwd']=$this->post['member_passwd'];
		$condition['member_state']=1; //1为启用2为关闭
		$condition['_logic'] = 'and';
		$count=$Model->where($condition)->count();
		if($count==0){
			return false;
		}
		return true;
	}

	//获取信息
	public function get_member_info()
	{
		$member_id=intval($this->member_id);
		//echo $member_id;exit;
		$Model=M("Member");
		$info=$Model->where('member_id='.$member_id)->find();
		//print_r($info);exit;
		return $info;
	}
	
	//判断用户密码是否正确
	public function check_password()
	{
		$member_passwd=$this->post['member_passwd'];
		$Model=M("Member");
		$where['member_id']=$this->post['member_id'];
		//$condition['_logic'] = 'and';
		// 把查询条件传入查询方法
		$right_member_passwd=$Model->where($where)->getField('member_passwd');
		if($member_passwd!=$right_member_passwd){
			return false;
		}
		return true;
	}
	//修改密码
	public function edit_password()
	{
		$member_passwd=md5($this->post['member_passwd']);
		$Model=M("Member");
		$where['member_id']=$this->post['member_id'];
		//$condition['_logic'] = 'and';
		$data['member_passwd']=$member_passwd;
		// 把查询条件传入查询方法
		$result=$Model->where($where)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//判断邮箱是否存在 true表示存在
	public function check_email_exist()
	{
		$Model=M("Member");
		$where['member_email']=$this->post['member_email'];
		$count=$Model->where($where)->count();
		if($count==0){
			return false;
		}
		return true;
	}
	//判断是否存在 true表示存在
	public function check_mobile_exist()
	{
		$Model=M("Member");
		$where['member_mobile']=$this->post['member_mobile'];
		$count=$Model->where($where)->count();
		if($count==0){
			return false;
		}
		return true;
	}
	//判断是否存在 true表示存在
	public function check_idcard_num_exist()
	{
		$Model=M("Member");
		$where['member_idcard_num']=$this->post['member_idcard_num'];
		$where['member_id']=$this->post['member_id'];
		$count=$Model->where($where)->count();
		if($count==0){
			return false;
		}
		return true;
	}
	//修改密码
	public function save_member()
	{
		$Model=M("Member");
		$where['member_id']=$this->post['member_id'];
		$data=$this->post;
		$result=$Model->where($where)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}

}